<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>寄生组合式继承</title>
	</head>
	<body>

        
		<script>
			function inheritPrototype(Son, Father) {
				var prototype = Object.create(Father.prototype) // 创建对象，创建父类原型的一个副本
				prototype.constructor = Son // 增强对象，弥补因重写原型而失去的默认的constructor 属性
				Son.prototype = prototype // 指定对象，将新创建的对象赋值给子类的原型
			}
0
			// 父类初始化实例属性和原型属性
			function Father(name) {
				this.name = name
				this.colors = ['red', 'blue', 'green']
			}
			Father.prototype.sayName = function () {
				console.log(this.name)
			}

			// 借用构造函数传递增强子类实例属性（支持传参和避免篡改）
			function Son(name, age) {
				Father.call(this, name)
				this.age = age
			}

			// 将父类原型指向子类
			inheritPrototype(Son, Father)

			// 新增子类原型属性
			Son.prototype.sayAge = function () {
				console.log(this.age)
			}

			var instance1 = new Son('xyc', 23)
			var instance2 = new Son('lxy', 23)

			instance1.colors.push('2') // ["red", "blue", "green", "2"]
            console.log(instance1)
			instance1.colors.push('3') // ["red", "blue", "green", "3"]
            console.log(instance1)


            
		</script>
	</body>
</html>
